package com.triones.ljr.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysUserImportVo;
import com.triones.ljr.entity.dto.EnterpriseRecommendDO;
import com.triones.ljr.entity.dto.HandleAppDO;
import com.triones.ljr.entity.vo.EnterpriseRecommendImportVo;
import com.triones.ljr.entity.vo.EnterpriseRecommendVO;
import com.triones.ljr.entity.vo.HandleAppImportVO;
import com.triones.ljr.mapper.HandleAppMapper;
import com.triones.ljr.mapper.HandleCategorizeMapper;
import com.triones.ljr.service.EnterpriseRecommendService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 推荐企业
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/enterpriseRecommend")
public class EnterpriseRecommendController extends BaseController {

    private final EnterpriseRecommendService enterpriseRecommendService;

    private final HandleAppMapper handleAppMapper;

    /**
     * 列表（分页）
     */
    @SaCheckPermission("system:enterpriseRecommend:pageList")
    @GetMapping("/pageList")
    public TableDataInfo<EnterpriseRecommendVO> pageList(EnterpriseRecommendDO dto, PageQuery pageQuery) {
        return enterpriseRecommendService.pageList(dto, pageQuery);
    }

    /**
     * 新增
     */
    @SaCheckPermission("system:enterpriseRecommend:add")
    @Log(title = "推荐企业", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody EnterpriseRecommendDO dto) {
        if (!enterpriseRecommendService.checkNameUnique(dto)) {
            return R.fail("新增企业'" + dto.getName() + "'失败，企业名称已存在");
        }
        return toAjax(enterpriseRecommendService.add(dto));
    }

    /**
     * 修改
     */
    @SaCheckPermission("system:enterpriseRecommend:edit")
    @Log(title = "推荐企业", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody EnterpriseRecommendDO dto) {
        if (!enterpriseRecommendService.checkNameUnique(dto)) {
            return R.fail("修改企业'" + dto.getName() + "'失败，企业名称已存在");
        }
        return toAjax(enterpriseRecommendService.edit(dto));
    }

    /**
     * 获取详情
     */
    @GetMapping("/{id}")
    @SaCheckPermission("system:enterpriseRecommend:query")
    public R<EnterpriseRecommendVO> getInfo(@PathVariable Long id) {
        return R.ok(enterpriseRecommendService.getInfo(id));
    }

    /**
     * 删除
     */
    @SaCheckPermission("system:enterpriseRecommend:remove")
    @Log(title = "推荐企业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(enterpriseRecommendService.remove(id));
    }

    /**
     * 导入数据
     *
     * @param file 导入文件
     */
    @Log(title = "推荐企业", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:enterpriseRecommend:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<EnterpriseRecommendImportVo> excelResult = ExcelUtil.importExcel(file.getInputStream(), EnterpriseRecommendImportVo.class, true);
        List<EnterpriseRecommendImportVo> voList = excelResult.getList();
        List<EnterpriseRecommendDO> list = new ArrayList<>();
        if (voList != null && voList.size() > 0) {
            for (EnterpriseRecommendImportVo vo : voList) {
                EnterpriseRecommendDO byName = enterpriseRecommendService.selectByName(vo.getName());
                if (byName == null) {
                    EnterpriseRecommendDO dto = new EnterpriseRecommendDO();
                    dto.setCreateTime(DateUtil.getDateTimeString());
                    BeanUtil.copyProperties(vo, dto);
                    list.add(dto);
                }
            }
        }
        enterpriseRecommendService.saveBatch(list);
        return R.ok(excelResult.getAnalysis());

//        ExcelResult<HandleAppImportVO> excelResult = ExcelUtil.importExcel(file.getInputStream(), HandleAppImportVO.class, true);
//        List<HandleAppImportVO> list = excelResult.getList();
//        List<HandleAppDO> addList = new ArrayList<>();
//        for (HandleAppImportVO handleAppImportVO : list) {
//            HandleAppDO handleAppDO = new HandleAppDO();
//            switch (handleAppImportVO.getType()) {
//                case "开办企业（注销）":
//                    handleAppDO.setHcId(1945035095064088577L);
//                    break;
//                case "办理建筑许可":
//                    handleAppDO.setHcId(1945035249531916290L);
//                    break;
//                case "不动产登记":
//                    handleAppDO.setHcId(1945036728468672513L);
//                    break;
//                case "获得用水":
//                    handleAppDO.setHcId(1945036795678199810L);
//                    break;
//                case "获得用电":
//                    handleAppDO.setHcId(1945036867669233665L);
//                    break;
//                case "获得用网":
//                    handleAppDO.setHcId(1945037363108810754L);
//                    break;
//                case "集成服务":
//                    handleAppDO.setHcId(1945037411234254850L);
//                    break;
//                case "政府采购":
//                    handleAppDO.setHcId(1945037465546297345L);
//                    break;
//                case "招标投标":
//                    handleAppDO.setHcId(1945037956690907138L);
//                    break;
//                case "市场监管":
//                    handleAppDO.setHcId(1945038089436434433L);
//                    break;
//                case "金融服务":
//                    handleAppDO.setHcId(1945038147619819522L);
//                    break;
//                case "劳动就业":
//                    handleAppDO.setHcId(1945038214648991745L);
//                    break;
//                case "执行合同":
//                    handleAppDO.setHcId(1945038292365250562L);
//                    break;
//                case "创新发展":
//                    handleAppDO.setHcId(1945038364276592642L);
//                    break;
//                case "信用体系建设":
//                    handleAppDO.setHcId(1945038425563762690L);
//                    break;
//                case "获得用气":
//                    handleAppDO.setHcId(1947459270318252033L);
//                    break;
//            }
//            handleAppDO.setName(handleAppImportVO.getName());

            /**
             * 线下资料
             */
//            handleAppDO.setAgencyName(handleAppImportVO.getAgencyName());
//            handleAppDO.setHandleAddress(handleAppImportVO.getHandleAddress());
//            handleAppDO.setHandlePhone(handleAppImportVO.getHandlePhone());
//            handleAppDO.setHandleTime(handleAppImportVO.getHandleTime());
//            handleAppDO.setIsApp("1");
//            addList.add(handleAppDO);
            /**
             * 应用资料
             */
//            handleAppDO.setAppName(handleAppImportVO.getAppName());
//            handleAppDO.setAppDescribe(handleAppImportVO.getAppDescribe());
//            handleAppDO.setAppRouteUrl(handleAppImportVO.getAppRouteUrl());
//            handleAppDO.setIsApp("0");
//            String appType = handleAppImportVO.getAppType();
//            String[] split = appType.split("、");
//            if ( split.length == 1) {
//                handleAppDO.setAppType(appType);
//                addList.add(handleAppDO);
//            } else {
//                for (String s : split) {
//                    HandleAppDO handleAppDO1 = BeanUtil.toBean(handleAppDO, HandleAppDO.class);
//                    handleAppDO1.setAppType(s);
//                    addList.add(handleAppDO1);
//                }
//            }
//        }
//        handleAppMapper.insertBatch(addList);
//        return R.ok(excelResult.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "推荐企业数据", EnterpriseRecommendImportVo.class, response);
    }
}
